package inventoryRecord

import (
	"context"
	"github.com/google/uuid"
	"insighttracking/apps/admin/auth"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetInventoryRecordDetailPageApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetInventoryRecordDetailPageApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetInventoryRecordDetailPageApiLogic {
	hostname, _ := os.Hostname()
	return &GetInventoryRecordDetailPageApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetInventoryRecordDetailPageApiLogic) GetInventoryRecordDetailPageApi(req *types.GetInventoryRecordDetailPageReq) (resp *types.GetInventoryRecordDetailPageResp, err error) {
	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return nil, err
	}
	result, count, err := service.InventRecordService.GetInventoryRecordDetailPageApi(l.ctx, bo.GetInventoryRecordDetailPageBoReq{
		SpuID:        req.SpuID,                // 商品id,关联到spu表主键id
		StartDate:    req.StartDate,            // 开始时间  时间格式:2024-12-12 12:12:12
		EndDate:      req.EndDate,              // 结束时间  时间格式:2024-12-12 12:12:12
		Types:        req.Types,                // 类型,0表示入库,1表示出库
		WarehouseID:  req.WarehouseID,          // 类型,0表示入库,1表示出库
		PageSize:     req.PageSize,             // 当前条数
		PageNumber:   req.PageNumber,           // 当前页数
		EnterpriseID: accountInfo.EnterpriseId, // 关联到enterprise表主键id
	})
	var getInventoryRecord = make([]types.GetInventoryRecordResp, 0)
	for _, item := range result {
		getInventoryRecord = append(getInventoryRecord, types.GetInventoryRecordResp{
			ID:           item.ID,                                 // 主键id
			EnterpriseID: item.EnterpriseID,                       // 关联到enterprise表主键id
			SpuID:        item.SpuID,                              // 商品主键,关联到spu表主键id
			Types:        item.Types,                              // 类型,0表示入库,1表示出库
			Source:       item.Source,                             // 来源,0表示正常出入口,10表示盘点报损报溢,20表调拨
			WarehouseID:  item.WarehouseID,                        // 仓库id,关联到warehouse表主键id
			ChangeCount:  item.ChangeCount.StringFixed(2),         // 变动数量
			Price:        item.Price.StringFixed(2),               // 单价
			Amount:       item.Amount.StringFixed(2),              // 金额
			Remark:       item.Remark,                             // 备注
			CreatedAt:    utils.TimestampToString(item.CreatedAt), // 创建时间
			UpdatedAt:    utils.TimestampToString(item.UpdatedAt), // 更新时间
		})
	}
	return &types.GetInventoryRecordDetailPageResp{
		Data: getInventoryRecord,
		PageBaseResp: types.PageBaseResp{
			Total:      count,
			PageNumber: req.PageNumber,
			PageSize:   req.PageSize,
		},
	}, nil
}
